/*
 SGSortingGameLevel.m
 Date Created: March 6, 2010
 
 Authors: Karanvir Gill <viper1029@gmail.com>
          Frank Lau <flaua@sfu.ca>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */


#import "SGSortingGameLevel.h"
#import "SGRecyclableType.h"
#import "SGSortingGame.h"

@implementation SGSortingGameLevel
@dynamic level;
@dynamic livesLeft;
@dynamic score;

@dynamic numItemsDisplayed;
@dynamic numItemsDroppedInWrongBin;
@dynamic numItemsDroppedInCorrectBin;
@dynamic totalNumItems;

@dynamic owner;

- (void)incrementScore:(NSUInteger)score {
  self.score+=score;
  self.owner.score+=score;
}

- (void)itemDropped:(BOOL)correctly {
  if(correctly==YES) {
    self.numItemsDroppedInCorrectBin++;
    self.owner.numItemsCorrectlySorted++;
  } else {
    self.numItemsDroppedInWrongBin++;
    self.livesLeft--;
  }
  
  self.owner.numItemsProcessed++;
}

- (float)accuracy {
  float levelAccuracy = ((float)self.numItemsDroppedInCorrectBin/(float)self.totalNumItems)*100;
  return levelAccuracy;
}


- (float)speedOfItems {
  return pow(1.15,self.level-1); //no x movement
}

- (NSUInteger)level {
  [self willAccessValueForKey:@"level"];
  NSNumber *levelObj=[self primitiveValueForKey:@"level"];
  [self didAccessValueForKey:@"level"];
  return levelObj.unsignedIntValue;
}

- (void)setLevel:(NSUInteger)newLevel {
  self.totalNumItems = 15+3*(newLevel-1);
  [self willChangeValueForKey:@"level"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newLevel] forKey:@"level"];
  [self didChangeValueForKey:@"level"];
}

- (NSUInteger)livesLeft {
  [self willAccessValueForKey:@"livesLeft"];
  NSNumber *livesLeftObj=[self primitiveValueForKey:@"livesLeft"];
  [self didAccessValueForKey:@"livesLeft"];
  return livesLeftObj.unsignedIntValue;
}

- (void)setLivesLeft:(NSUInteger)newLivesLeft {  
  [self willChangeValueForKey:@"newLivesLeft"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newLivesLeft] forKey:@"livesLeft"];
  [self didChangeValueForKey:@"livesLeft"];
}

- (NSUInteger)score {
  [self willAccessValueForKey:@"score"];
  NSNumber *scoreObj=[self primitiveValueForKey:@"score"];
  [self didAccessValueForKey:@"score"];
  return scoreObj.unsignedIntValue;
}

- (void)setScore:(NSUInteger)newScore {  
  [self willChangeValueForKey:@"score"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newScore] forKey:@"score"];
  [self didChangeValueForKey:@"score"];
}

- (NSUInteger)numItemsDisplayed {
  [self willAccessValueForKey:@"numItemsDisplayed"];
  NSNumber *numItemsDisplayedObj=[self primitiveValueForKey:@"numItemsDisplayed"];
  [self didAccessValueForKey:@"numItemsDisplayed"];
  return numItemsDisplayedObj.unsignedIntValue;
}

- (void)setNumItemsDisplayed:(NSUInteger)newNumItemsDisplayed {  
  [self willChangeValueForKey:@"numItemsDisplayed"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newNumItemsDisplayed] forKey:@"numItemsDisplayed"];
  [self didChangeValueForKey:@"numItemsDisplayed"];
}

- (NSUInteger)numItemsDroppedInWrongBin {
  [self willAccessValueForKey:@"numItemsDroppedInWrongBin"];
  NSNumber *numItemsDroppedInWrongBinObj=[self primitiveValueForKey:@"numItemsDroppedInWrongBin"];
  [self didAccessValueForKey:@"numItemsDroppedInWrongBin"];
  return numItemsDroppedInWrongBinObj.unsignedIntValue;
}

- (void)setNumItemsDroppedInWrongBin:(NSUInteger)newNumItemsDroppedInWrongBin {  
  [self willChangeValueForKey:@"numItemsDroppedInWrongBin"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newNumItemsDroppedInWrongBin] forKey:@"numItemsDroppedInWrongBin"];
  [self didChangeValueForKey:@"numItemsDroppedInWrongBin"];
}

- (NSUInteger)numItemsDroppedInCorrectBin {
  [self willAccessValueForKey:@"numItemsDroppedInCorrectBin"];
  NSNumber *numItemsDroppedInCorrectBinObj=[self primitiveValueForKey:@"numItemsDroppedInCorrectBin"];
  [self didAccessValueForKey:@"numItemsDroppedInCorrectBin"];
  return numItemsDroppedInCorrectBinObj.unsignedIntValue;
}

- (void)setNumItemsDroppedInCorrectBin:(NSUInteger)newNumItemsDroppedInCorrectBin {  
  [self willChangeValueForKey:@"numItemsDroppedInWrongBin"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newNumItemsDroppedInCorrectBin] forKey:@"numItemsDroppedInCorrectBin"];
  [self didChangeValueForKey:@"numItemsDroppedInCorrectBin"];
}

- (NSUInteger)totalNumItems {
  [self willAccessValueForKey:@"totalNumItems"];
  NSNumber *totalNumItemsObj=[self primitiveValueForKey:@"totalNumItems"];
  [self didAccessValueForKey:@"totalNumItems"];
  return totalNumItemsObj.unsignedIntValue;
}

- (void)setTotalNumItems:(NSUInteger)newTotalNumItems {  
  [self willChangeValueForKey:@"totalNumItems"];
  [self setPrimitiveValue:[NSNumber numberWithUnsignedInt:newTotalNumItems] forKey:@"totalNumItems"];
  [self didChangeValueForKey:@"totalNumItems"];
}

- (NSArray *)listItemsInLevel {
  NSRange itemRange;
  NSArray *avaliableRecyclableTypes = [SGRecyclableType listOfficialTypes];
	switch(self.level) {
    case 1:
      itemRange.location=0;
      itemRange.length = 4;	
      break;
    case 2:
      itemRange.location=0;
      itemRange.length = 6;	
      break;
    case 3:
      itemRange.location=0;
      itemRange.length = 8;	
      break;
    case 4:
      itemRange.location=0;
      itemRange.length = 9;	
      break;
    case 5:
      itemRange.location=0;
      itemRange.length = 11;	
      break;
    default:
      itemRange.location=0;
      itemRange.length = 13;	
      break;
  }
	
  NSArray *itemsInLevel = [avaliableRecyclableTypes subarrayWithRange:itemRange];
  return itemsInLevel;
}

- (SGRecyclableItem *)popNextItem {
  if(self.numItemsDisplayed==self.totalNumItems) return nil;
  self.numItemsDisplayed++;
  
  SGRecyclableItem *item=(SGRecyclableItem *)[NSEntityDescription insertNewObjectForEntityForName:@"SortingGameItem"
                                                                           inManagedObjectContext:self.managedObjectContext];
  NSArray *avaliableItemsInCurrentLevel = [self listItemsInLevel];
  SGRecyclableType *type=[avaliableItemsInCurrentLevel objectAtIndex:(arc4random() % avaliableItemsInCurrentLevel.count)];
  item.type=type;
  [self.owner addItemsObject:item];
  
  return item;
}
@end
